<template>
	<view>
		<van-search
		  placeholder="公司名"
		  use-action-slot
		  @change="onChange"
		  @search="onSearch"
		>
		  <!-- <view slot="action" @tap="addNewCompany">新增公司</view> -->
		</van-search>
		
		<van-cell-group title="公司列表">
			<van-cell v-for="(item, index) in companys" :key="index" :title="item.companyName" is-link>
				<view style="display: flex;flex-flow: row;justify-content: flex-end;">
					<van-button :data-item="item" :color="primaryColor" :data-cid="item.companyId" size="mini" @click="onForward">转发</van-button>
					<view style="margin-left: 10px;"></view>
					<van-button :data-item="item" :color="primaryColor" size="mini" @click.stop.prevent="onGotoCompany" plain>详情</van-button>
				</view>
			</van-cell>
		</van-cell-group>
		
		<van-dialog id="van-dialog" />
		<van-toast id="van-toast" />
	</view>
</template>

<script>
	import utils from '@/common/utils.js'
	import Dialog from '@/wxcomponents/dialog/dialog'
	import Toast from '@/wxcomponents/toast/toast'
	export default {
		data() {
			return {
				primaryColor: getApp().globalData.primaryColor,
				companys:[],
				orderId:null
			};
		},
		async onLoad(options){
			this.orderId = options.orderid
			await this.getCompany()
		},
		methods:{
			onChange(){
				
			},
			onSearch(event){
				const str = event.detail
				this.companys = this.companys.filter(o => o.companyName.includes(str))
			},
			async getCompany(){
				try{
					const rsp = await utils.req('/user/getThirdCompany', {token: uni.getStorageSync('userinfo').token})
					this.companys = rsp
				}catch(e){
					//TODO handle the exception
				}
			},
			onForward(e){
				
				console.log(e)
				const cid = e.currentTarget.dataset.cid
				const reqData = {
					token: uni.getStorageSync('userinfo').token,
					orderId: this.orderId,
					thirdCompanyId: cid
				}
				Dialog.confirm({
					title: '提示',
					message: '确认转发此申请?'
				}).then(async() => {
					try{
						uni.showLoading({
							mask:true,
							title:'上传数据中...'
						})
						
						await utils.req('/order/dispatchThirdCompany', reqData)
						Toast({
						  message: '转发成功',
						  onClose: () => {
						    uni.navigateBack({
						    	delta:1
						    })
						  }
						})
						
					}catch(e){
						console.error(e)
						Toast(e.msg)
					}finally{
						uni.hideLoading()
					}
				}).catch(() => {
					// on cancel
				});
			},
			addNewCompany(){
				uni.navigateTo({
					url:'../company-add/company-add'
				})
			},
			onGotoCompany(event){
				const enData = encodeURIComponent(JSON.stringify(event.currentTarget.dataset.item))
				uni.navigateTo({
					url:`../company-detail/company-detail?data=${enData}`
				})
			},
			onCall(){
				
			}
		}
	}
</script>

<style lang="scss">

</style>
